#include <cstdlib>
#include <iostream>
#include "functions.h"
#include "80disasm.h"
//#include "constants.h"
using namespace std;

int main(int argc, char *argv[])
{
    
    char* filename=new char[255];
    string op;  
    
    
    
    
//if argc it's bigger than 4,this means that the user started the program with commando line argumens
//this command are just necesssary if the user wanna use the 8080disassembler by another program    
if(argc>=3)
{
           
           cout<<argv[1]<<endl<<argv[2];
           
    int errors=0;
    
    if(!file_exists(argv[1]))
    {
                             cout<<"Eror: the file "<<argv[1]<<"doesn't exist\n";
                             ++errors;
    }
    
    if(argv[2]!="s" && argv[2]!="S" && argv[2]!="n" && argv[2]!="N")
    {
                             cout<<"Error: Invalid commands\n";
                             ++errors;
    }
    
    if(errors==0)
    {
                 
                 
        filename=argv[1];
        
        if(argv[2]=="s" || argv[2]=="S")
        op="y";
    }
    else
    {
    exit(1);    
    }
    
    
}
 
 
 show_header_text();
 if(argc<=1)
 {
    
    do
    {
    cout<<"Introduce the filename you want to disassembler:\n";    
    cin>>filename;
    
    if(!file_exists(filename))
    {
                              cout<<"The file you introduced doesn't exist.\n\n";
    }
    else
    {
    break;    
    }
    
    }while(1);
    
    
    cout<<"Do you wanna to save the assembly code to a file? (Y/N)\n";
    do
    {
              cin>>op;
    }
    while(op!="Y" && op!="y" && op!="N" && op!="n");
    
    
}//end argc<1
   
    
    
    
    int* memory=load_to_memory(filename);
    
   _80disasm obj(memory,getFileSize(filename));
    
    
     if(op=="Y" || op=="y")
    {
        obj.setExport(true);
    }
    
    
    obj.exec(); //here the assembler starts
    cout<<"\n--------8080 ASSEMBLY OUTPUT--------\n\n";
    cout<<obj.getOutput();    
     cout<<"\n--------------finished-------------\n";
    delete[] memory; //these instructions will frreem the ram memory
    
    
    
        
    cin.get();
    cin.get();
}
